<template>
  <div class="body">
    <!-- 登录注册容器 -->
    <div class="container" :class="{ active: isActive }">
      <!--  登录  -->
      <div class="form-box login">
        <el-form style="width: 100%" status-icon ref="fromRef" :model="data.form" :rules="data.rules"
                 :validate-on-rule-change="false"
                 :key="isActive"
                 @submit.prevent> <!-- 阻止默认提交 -->
          <h1>登录</h1>
          <el-form-item class="input-box" prop="username">
            <el-input size="large" v-model="data.form.username" placeholder="请输入账号" autocomplete="off" prefix-icon="User" />
          </el-form-item>
          <el-form-item class="input-box" prop="password">
            <el-input size="large" show-password v-model="data.form.password" placeholder="请输入密码" autocomplete="off" prefix-icon="Lock" />
          </el-form-item>
          <el-form-item class="input-box" prop="role">
            <el-select size="large" style="width: 100%" v-model="data.form.role" placeholder="请选择角色">
              <el-option label="用户" value="USER">用户</el-option>
              <el-option label="管理员" value="ADMIN">管理员</el-option>
            </el-select>
          </el-form-item>
          <div>
            <el-button style="width: 100%; height: 48px" type="primary" size="large" @click="login">登录</el-button>
          </div>
        </el-form>
      </div>

      <!--  注册  -->
      <div class="form-box register">
        <el-form style="width: 100%" status-icon ref="fromRef" :model="data.form" :rules="data.rules"
                 :validate-on-rule-change="false"
                 :key="isActive"
                 @submit.prevent>
          <h1>注册</h1>
          <el-form-item class="input-box" prop="username">
            <el-input size="large" v-model="data.form.username" placeholder="请输入账号" autocomplete="off" prefix-icon="User" />
          </el-form-item>
          <el-form-item class="input-box" prop="password">
            <el-input size="large" show-password v-model="data.form.password" placeholder="请输入密码" autocomplete="off" prefix-icon="Lock" />
          </el-form-item>
          <el-form-item class="input-box" prop="confirmPassword">
            <el-input size="large" show-password v-model="data.form.confirmPassword" placeholder="请再次确认密码" autocomplete="off" prefix-icon="Lock" />
          </el-form-item>
          <div>
            <el-button style="width: 100%" type="primary" size="large" @click="register">注册</el-button>
          </div>
        </el-form>
      </div>

      <!-- 切换面板 -->
      <div class="toggle-box">
        <div class="toggle-panel toggle-left">
          <h1>欢迎来到快递管理系统</h1>
          <p>还没有账号？</p>
          <el-button class="btn login-btn" type="primary" size="large" @click="toggleForm(true)">注册</el-button>
        </div>

        <div class="toggle-panel toggle-right">
          <h1>已有账号？</h1>
          <p>返回</p>
          <el-button class="btn register-btn" type="primary" size="large" @click="toggleForm(false)">登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const fromRef = ref()

// 设备检测函数
const isMobile = () => {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
}

const validatePass = (rule, value, callback) => {
  // value 表示用户输入的确认密码
  if (value !== data.form.password) {
    callback(new Error("两次密码不相同"))
  } else {
    callback()
  }
}

const loginRules = {
  username: [
    { required: true, message: '账号未输入', trigger: 'blur' },
    { min: 3, message: '账号长度不能小于3位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ]
}

const registerRules = {
  username: [
    { required: true, message: '账号未输入', trigger: 'blur' },
    { min: 3, message: '账号长度不能小于3位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' },
  ]
}

const data = reactive({
  form: { role: 'USER' },
  rules: loginRules
})

// 控制表单切换状态
const isActive = ref(false)

// 切换表单类型
const toggleForm = (showRegister) => {
  isActive.value = showRegister
  data.rules = showRegister ? registerRules : loginRules // 动态切换验证规则
  fromRef.value?.clearValidate() // 切换时清空验证状态
}

// 登录逻辑
const login = () => {
  fromRef.value.validate((valid) => {
    if (valid){
      request.post('/login', data.form).then(res => {
        if (res.code === '200') {
          // 存储用户信息
          localStorage.setItem('code_user', JSON.stringify(res.data || {}))
          ElMessage.success('登录成功')
          let targetRoute = '/manager/home';
          if (isMobile()) {
            // 设备检测逻辑
            targetRoute = data.form.role === 'USER'
                ? '/mobileHome'
                : '/mobileAdmin';
          }

          // 存储设备类型到本地
          localStorage.setItem('device_type', isMobile() ? 'mobile' : 'desktop')
          router.push(targetRoute)
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

// 注册逻辑
const register = () => {
  fromRef.value.validate((valid) => {
    if (valid){
      request.post('/register', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success('注册成功')
          router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}
</script>

<style scope>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(90deg, #e2e2e2, #c9d6ff);
}

.container {
  position: relative;
  width: 850px;
  height: 550px;
  background-color: #fff;
  border-radius: 30px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  margin: 20px;
  overflow: hidden;
}

.form-box {
  position: absolute;
  right: 0;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  color: #333;
  text-align: center;
  padding: 40px;
  z-index: 1;
  transition: 0.6s ease-in-out 1.2s, visibility 0s 1s;
}

.container.active .form-box {
  right: 50%;
}

.form-box.register {
  visibility: hidden;
}

.container.active .form-box.register {
  visibility: visible;
}

.container h1 {
  font-size: 36px;
  text-align: center;
  margin: -10px 0;
}

.input-box {
  position: relative;
  margin: 30px 0;
}

.btn {
  width: 160px;
  height: 46px;
  background-color: transparent;
  border: 2px solid #fff;
  box-shadow: none
}

.toggle-box {
  position: absolute;
  width: 100%;
  height: 100%;
}

.toggle-box::before{
  content: '';
  position: absolute;
  left: -250%;
  width: 300%;
  height: 100%;
  background-color: #7494ec;
  border-radius: 150px;
  z-index: 2;
  transition: 1.8s ease-in-out;
}

.container.active .toggle-box::before{
  left: 50%;
}

.toggle-panel {
  position: absolute;
  width: 50%;
  height: 100%;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  transition: .6s ease-in-out;
}

.toggle-panel.toggle-left{
  left: 0;
  transition-delay: 1.2s;
}

.container.active .toggle-panel.toggle-left{
  left: -50%;
  transition-delay: .6s;
}

.toggle-panel.toggle-right {
  right: -50%;
  transition-delay: .6s;
}

.container.active .toggle-panel.toggle-right{
  right: 0;
  transition-delay: 1.2s;
}

.toggle-panel p{
  font-size: 15px;
  margin: 15px 0 20px 0;
}

@media screen and (max-width: 650px) {
  .container {
    width: 80vw;
    height: calc(100vh - 40px);
  }

  .form-box {
    bottom: 0;
    width: 100%;
    height: 70%;
  }

  .container.active .form-box {
    right: 0;
    bottom: 30%;
  }

  .toggle-box::before {
    left: 0;
    width: 100%;
    top: -270%;
    height: 300%;
    border-radius: 20vw;
  }

  .container.active .toggle-box::before {
    left: 0;
    top: 70%;
  }

  .toggle-panel {
    width: 100%;
    height: 30%;
  }

  .toggle-panel.toggle-left {
    top: 0;
  }

  .container.active .toggle-panel.toggle-left {
    left: 0;
    top: -30%;
  }

  .toggle-panel.toggle-right {
    right: 0;
    bottom: -30%;
  }

  .container.active .toggle-panel.toggle-right {
    bottom: 0;
  }
}

@media screen and (max-width: 400px) {
  .form-box {
    padding: 20px;
  }

  .toggle-panel h1 {
    font-size: 30px;
  }
}
</style>


